//控制层 
app.controller('productController', function($scope, $controller, $location, $http, productCateService, productService) {

	$controller('baseController', {
		$scope: $scope
	}); //继承

	//读取列表数据绑定到表单中  
	$scope.findAll = function() {
		productService.findAll().success(
			function(response) {
				$scope.list = response;
			}
		);
	}

	//分页
	$scope.findPage = function(page, rows) {
		productService.findPage(page, rows).success(
			function(response) {
				$scope.list = response.rows;
				$scope.paginationConf.totalItems = response.total; //更新总记录数
			}
		);
	}

	//查询实体 
	$scope.findOne = function(id) {
		productService.findOne(id).success(
			function(response) {
				$scope.entity = response;
			}
		);
	}

	$scope.litimgs = [];
	//保存或修改 -----------------------------------------
	$scope.save = function() {
		var serviceObject; //服务层对象 
		if($scope.entity.id != null) { //如果有ID
			//初始化
			$scope.litimgs = [];
			//修改产品缩略图信息 , 根据 先根据 产品id删除所有 当前商品所有缩略图 , 再根据判断 重新上传并录入缩略图
			if($(".litimgs")[0].files[0] != null || $(".litimgs")[1].files[0] != null || $(".litimgs")[2].files[0] != null || $(".litimgs")[3].files[0] != null) {
				//修改产品及缩略图开始--------------------
				if($(".litimgs")[0].files[0] != null) {
					/*var file = document.querySelector('input[type=file]').files[0];*/
					var file = document.getElementsByClassName("litimgs")[0].files[0];
					if(file != null) {
						//设置表单
						var formData = new FormData();
						formData.append('file', file);
						$.ajax({
							url: '/file/upload.do',
							type: 'POST',
							async: false,
							data: formData,
							cache: false,
							contentType: false, //不可缺
							processData: false, //不可缺
							success: function(data) {
								$scope.litimgs.push(data.data.src);
							}
						});
					}
				}else{
					if($scope.litimgList[0] != null){
						$scope.litimgs.push($scope.litimgList[0].litimg);
					}
				}
				
				if($(".litimgs")[1].files[0] != null) {
					/*var file = document.querySelector('input[type=file]').files[0];*/
					var file = document.getElementsByClassName("litimgs")[1].files[0];
					if(file != null) {
						//设置表单
						var formData = new FormData();
						formData.append('file', file);
						$.ajax({
							url: '/file/upload.do',
							type: 'POST',
							async: false,
							data: formData,
							cache: false,
							contentType: false, //不可缺
							processData: false, //不可缺
							success: function(data) {
								$scope.litimgs.push(data.data.src);
							}
						});
					}
				}else{
					if($scope.litimgList[1] != null){
						$scope.litimgs.push($scope.litimgList[1].litimg);
					}
				}
				if($(".litimgs")[2].files[0] != null) {
					/*var file = document.querySelector('input[type=file]').files[0];*/
					var file = document.getElementsByClassName("litimgs")[2].files[0];
					if(file != null) {
						//设置表单
						var formData = new FormData();
						formData.append('file', file);
						$.ajax({
							url: '/file/upload.do',
							type: 'POST',
							async: false,
							data: formData,
							cache: false,
							contentType: false, //不可缺
							processData: false, //不可缺
							success: function(data) {
								$scope.litimgs.push(data.data.src);
							}
						});
					}
				}else{
					if($scope.litimgList[2] != null){
						$scope.litimgs.push($scope.litimgList[2].litimg);
					}
				}
				if($(".litimgs")[3].files[0] != null) {
					/*var file = document.querySelector('input[type=file]').files[0];*/
					var file = document.getElementsByClassName("litimgs")[3].files[0];
					if(file != null) {
						//设置表单
						var formData = new FormData();
						formData.append('file', file);
						$.ajax({
							url: '/file/upload.do',
							type: 'POST',
							async: false,
							data: formData,
							cache: false,
							contentType: false, //不可缺
							processData: false, //不可缺
							success: function(data) {
								$scope.litimgs.push(data.data.src);
							}
						});
					}
				}else{
					if($scope.litimgList[3] != null){
						$scope.litimgs.push($scope.litimgList[3].litimg);
					}
				}
				
				
				//修改对应 产品 的缩略图
				productService.updateLitimg($scope.entity.id,$scope.litimgs).success(
					function(response) {
						if(response.success) {
							//修改成功信息
							alert(response.message);
							serviceObject = productService.update($scope.entity); //修改  
						} else {
							alert(response.message);
						}

						serviceObject.success(
							function(response) {
								if(response.success) {
									//重新查询 
									alert(response.message);
									window.location = "product-list.html"
								} else {
									alert(response.message);
								}
							}
						);
					}
				);
				//修改产品及缩略图结束--------------------
			} else {
				//只修改商品本身信息
				serviceObject = productService.update($scope.entity); //修改  
			}
			
			//修改结束--------------------------------------
		} else {
			//保存-------------------------------
			if($(".litimgs")[0].files[0] != null || $(".litimgs")[1].files[0] != null || $(".litimgs")[2].files[0] != null || $(".litimgs")[3].files[0] != null) {
				for(var i = 0; i < $(".litimgs").length; i++) {
					/*var file = document.querySelector('input[type=file]').files[0];*/
					var file = document.getElementsByClassName("litimgs")[i].files[0];
					if(file != null) {
						//设置表单
						var formData = new FormData();
						formData.append('file', file);
						$.ajax({
							url: '/file/upload.do',
							type: 'POST',
							async: false,
							data: formData,
							cache: false,
							contentType: false, //不可缺
							processData: false, //不可缺
							success: function(data) {
								$scope.litimgs.push(data.data.src);
							}
						});
					}
				}
				//保存 缩略图到 对应 产品
				productService.addLitimg($scope.litimgs).success(
					function(response) {
						if(response.success) {
							//保存
							alert("产品缩略图添加成功!");
							$scope.entity.id = response.message;
							serviceObject = productService.add($scope.entity); //增加 
						} else {
							alert(response.message);
						}

						serviceObject.success(
							function(response) {
								if(response.success) {
									//重新查询 
									alert(response.message);
									window.location = "product-list.html"
								} else {
									alert(response.message);
								}
							}
						);
					}
				);
			} else {
				//保存 产品信息本身 ----------------------------
				serviceObject = productService.add($scope.entity); //增加 
			}
		}
		serviceObject.success(
			function(response) {
				if(response.success) {
					//重新查询 
					alert(response.message);
					window.location = "product-list.html"
				} else {
					alert(response.message);
				}
			}
		);
	}
//保存或修改结束 -----------------------------------------	
	
	//上传图片并保存课程
	$scope.uploadAndSave = function() {
		//提交课程前先保存图片到数据库,并拿到相对路径
		if($scope.entity.image != null) {
			productService.upload().then(function(response) {
				//保存图片后进行课程信息和图片路径的保存
				$scope.entity.detailPage = response.data.data.src;
				$scope.save();
			});
		} else {
			$scope.save();
		}
	}

	$scope.searchEntity = {}; //定义搜索对象 

	//搜索
	$scope.search = function(page, rows) {
		productService.search(page, rows, $scope.searchEntity).success(
			function(response) {
				$scope.list = response.rows;
				$scope.paginationConf.totalItems = response.total; //更新总记录数
			}
		);
		$scope.categoryList();
	}

	//分类列表
	$scope.categoryList = function() {
		productCateService.findAll().success(
			function(response) {
				$scope.categoryList = response;
			}
		);
	}
	
	
	
	//编辑页面携带参数
	$scope.edit = function() {
		var id = $location.absUrl().split("?id=")[1];
		$scope.categoryList();
		$scope.findOne(id);
		productService.findLitimgsByProductId(id).success(
			function(response) {
				$scope.litimgList = response;
			}
		);
	}

	//批量删除 
	$scope.dele = function() {
		if($scope.selectIds.length < 1) {
			alert("请选中要删除的产品!");
		} else {
			if(confirm('确实要删除 ' + $scope.selectIds.length + ' 件产品吗?')) {
				//获取选中的复选框			
				productService.dele($scope.selectIds).success(
					function(response) {
						if(response.success) {
							alert(response.message);
							$scope.reloadList(); //刷新列表
							$scope.selectIds = [];
						} else {
							alert(response.message);
						}
					}
				);
			}
		}
	}

	//单个删除
	$scope.delsFlag = false;
	$scope.delsingle = function(id) {
		if(confirm('确实要删除该产品吗?')) {
			productService.dele(id).success(
				function(response) {
					if(response.success) {
						alert(response.message);
						$scope.reloadList();
						$scope.delsFlag = true;
						$timeout(function() {
							$scope.delsFlag = false;
						}, 2000);
					} else {
						alert(response.message);
					}
				}
			);
		}

	}

	//选中全部
	$scope.selectAll = function($event) {
		if($event.target.checked) { //如果是被选中,则增加到数组
			angular.forEach($scope.list, function(each) { //foreach循环，datalist是需要循环的数据列表，function后面需要加each
				$scope.selectIds.push(each.id); //将空数组中push新的数据，符合条件的数据的id值
			});
		} else {
			angular.forEach($scope.list, function(each) { //foreach循环，datalist是需要循环的数据列表，function后面需要加each
				var idx = $scope.selectIds.indexOf(each.id);
				$scope.selectIds.splice(idx, 1); //删除 
			});
		}
	}

	//添加缩略图按钮
	$scope.addImage1 = function() {
		angular.element('#imageShow1').click();
	}
	$scope.addImage2 = function() {
		angular.element('#imageShow2').click();
	}
	$scope.addImage3 = function() {
		angular.element('#imageShow3').click();
	}
	$scope.addImage4 = function() {
		angular.element('#imageShow4').click();
	}

});